package com.ruoyi.operator.service.impl;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.operator.utils.NeteaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.operator.mapper.OptShopMapper;
import com.ruoyi.operator.domain.OptShop;
import com.ruoyi.operator.service.IOptShopService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商户信息Service业务层处理
 * 
 * @author zhanghang
 * @date 2021-01-20
 */
@Service
public class OptShopServiceImpl implements IOptShopService
{
    @Autowired
    private OptShopMapper optShopMapper;

    /**
     * 查询商户信息
     *
     * @param shopId 商户信息ID
     * @return 商户信息
     */
    @Override
    public OptShop selectOptShopByShopId(String shopId) {
        return optShopMapper.selectOptShopByShopId(shopId);
    }

    /**
     * 查询商户信息列表
     *
     * @param optShop 商户信息
     * @return 商户信息
     */
    @Override
    public List<OptShop> selectOptShopList(OptShop optShop)
    {
        return optShopMapper.selectOptShopList(optShop);
    }

    /**
     * 新增商户信息
     *
     * @param optShop 商户信息
     * @return 结果
     */
    @Override
    public int insertOptShop(OptShop optShop)
    {
        String shopid = optShopMapper.createShopId();
        String netAccount = "shop" + shopid;
        String netToken = NeteaseUtil.registNeteaseID(netAccount);
        if (netToken.equals("414") || netToken.equals("999")) {
            throw new BusinessException("创建网易云信用户失败！");
        }
        String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        optShop.setShopId(shopid);
        optShop.setCreateTime(date);
        optShop.setNetAccount(netAccount);
        optShop.setNetToken(netToken);
        if (StringUtils.isEmpty(optShop.getShopImg())) {
            optShop.setShopImg((int) (Math.random() * 2) == 0
                    ? "https://nim.nosdn.127.net/MTY2NzA5NzI%3D%2FbmltYV8xMjQ1ODE3MTgyM18xNTgyNjA5NjM2ODY5XzYzN2E4YWI1LTM2NGYtNDFlOC1hZDZkLWJkMTBkYmQyMjZjZg%3D%3D?imageView&type=png"
                    : "https://nim.nosdn.127.net/MTY2NzA5NzI%3D%2FbmltYV8xMjQ1ODE3MTgyM18xNTgyNjA5NjM2ODY5XzczZmUwZWQ1LTZjOWUtNGRlOS1hZTAzLTY3YmFmNWFmZTI0NQ%3D%3D?imageView&type=png");
        }
        initShopInfo(optShop);
        return optShopMapper.insertOptShop(optShop);
    }

    /**
     * 修改商户信息
     *
     * @param optShop 商户信息
     * @return 结果
     */
    @Override
    public int updateOptShop(OptShop optShop)
    {
        return optShopMapper.updateOptShop(optShop);
    }

    /**
     * 删除商户信息对象
     *
     * @param shopIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOptShopByShopIds(String shopIds) {
        return optShopMapper.deleteOptShopByShopIds(Convert.toStrArray(shopIds));
    }

    /**
     * 删除商户信息信息
     *
     * @param shopId 商户信息ID
     * @return 结果
     */
    @Override
    public int deleteOptShopByShopId(String shopId) {
        return optShopMapper.deleteOptShopByShopId(shopId);
    }



    /**
     * 校验商户是否允许操作
     *
     * @param deptId 组织Id
     * @return 结果
     */
    public void checkUserAllowed(Long deptId)
    {
        SysUser currentUser = ShiroUtils.getSysUser();
        if (StringUtils.isNull(currentUser.getDeptId()) || !deptId.equals(currentUser.getDeptId()))
        {
            throw new BusinessException("不允许跨商户操作");
        }
    }



    private void initShopInfo(OptShop optShop) {
        String[] gameTypeArr = { "80010101", "80010102" };
        String[] shopRoomList = { "谜友疯人院", "谜友戏精团", "谜友推理社 ", "谜友灵魂馆" };
        for (String gametype : gameTypeArr) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("shopid", optShop.getShopId());
            map.put("gametype", gametype);
            optShopMapper.addShopGameType(map);
        }
        String rmtime = new SimpleDateFormat("yyyyMMdd").format(new Date());
        for (int i = 0; i < shopRoomList.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("shopid", optShop.getShopId());
            map.put("roomid", "rm" + optShop.getShopId() + rmtime + i);
            map.put("roomname", shopRoomList[i]);
            optShopMapper.addShopNewRoom(map);
        }
        optShopMapper.addInitialShopAccount(optShop);
    }

}